package spotGUI.ElementViewer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import elements.Comment;
import java.util.ResourceBundle;

public class CommentableViewDecorator extends JPanel {
	
	private Box commentsBox;
	private Box verticalBox;
	private JButton btnMostraTuttiI;
	private Box buttonBox;
	private Component horizontalGlue;
	private JLabel lblComments;
	
	public CommentableViewDecorator(JPanel commentableElement, ArrayList<Comment> comments) {
		
		setMaximumSize(new Dimension(510, 2147483647));
		setMinimumSize(new Dimension(500, 10));
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{309, 0};
		gridBagLayout.rowHeights = new int[]{11, 20, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		
		GridBagConstraints gbc_verticalBox = new GridBagConstraints();
		gbc_verticalBox.anchor = GridBagConstraints.NORTH;
		gbc_verticalBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_verticalBox.gridx = 0;
		gbc_verticalBox.gridy = 1;
		
		verticalBox = Box.createVerticalBox();
		add (verticalBox, gbc_verticalBox);
		
		buttonBox = Box.createHorizontalBox();
		
		lblComments = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("CommentableViewDecorator.lblComments.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblComments.setBorder(new EmptyBorder(0, 0, 0, 10));
		lblComments.setFont(new Font("Agency FB", Font.BOLD | Font.ITALIC, 21));
		buttonBox.add(lblComments);
		
		horizontalGlue = Box.createHorizontalGlue();
		buttonBox.add(horizontalGlue);
		btnMostraTuttiI = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("CommentableViewDecorator.btnMostraTuttiI.text")); //$NON-NLS-1$ //$NON-NLS-2$
		buttonBox.add(btnMostraTuttiI);
		btnMostraTuttiI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showAllComments();
			}
		});
		verticalBox.add(buttonBox);
		
		commentsBox = Box.createVerticalBox();
		verticalBox.add (commentsBox);
		
		GridBagConstraints gbc_element = new GridBagConstraints();
		gbc_element.fill = GridBagConstraints.BOTH;
		gbc_element.insets = new Insets(0, 0, 5, 0);
		gbc_element.gridx = 0;
		gbc_element.gridy = 0;
		add(commentableElement, gbc_element);
		
		if (comments.size() == 0)
			commentsBox.add(new JLabel (ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("CommentableViewDecorator.noComments")));
			
		for (int i = 0; i < comments.size(); i++) {
			CommentView view = new CommentView(comments.get(i));
			if (i < (comments.size()-3))
				view.setVisible(false);
			commentsBox.add(view);
		}
		
		
	}

	public void addComment (Comment comment) {
		commentsBox.add(new CommentView(comment));
	}
	
	private void showAllComments () {
		for (int i = 0; i < commentsBox.getComponentCount(); i++) {
			commentsBox.getComponent(i).setVisible(true);
		}
	}
	
}

